package com.imessage.service.impl;

import com.bean.BusinessGroup;
import com.bean.BusinessUsePhone;
import com.imessage.service.BaseService;
import com.imessage.service.PhoneService;
import com.imessage.type.BaseState;
import io.ebean.Expr;
import io.ebean.Expression;
import io.ebean.Query;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@Slf4j
@Service
public class PhoneServiceImpl implements PhoneService {

    @Autowired
    private BaseService baseService;

    @Override
    public int getBusinessUsePhoneCount(BusinessGroup businessGroup) {
        List<Expression> expressionList=new ArrayList<Expression>();
        if(businessGroup!=null){
            expressionList.add(Expr.eq("BusinessGroup", businessGroup));
        }
        expressionList.add(Expr.eq("state", BaseState.normal.getCode()));
        Query<BusinessUsePhone> query=baseService.createQuery(BusinessUsePhone.class, expressionList);
        return query.findCount();
    }

    @Override
    public List<BusinessUsePhone> findBusinessUsePhoneList(
            BusinessGroup businessGroup,int start, int length) {
        List<Expression> expressionList=new ArrayList<Expression>();
        if(businessGroup!=null){
            expressionList.add(Expr.eq("BusinessGroup", businessGroup));
        }
        expressionList.add(Expr.eq("state", BaseState.normal.getCode()));
        return  baseService.findList(
                BusinessUsePhone.class, expressionList,null,null,start,length);
    }

    @Override
    public List<BusinessUsePhone> findBusinessTestPhoneList() {
        List<Expression> expressionList=new ArrayList<Expression>();
        expressionList.add(Expr.eq("BusinessGroup.name","测试组"));
        expressionList.add(Expr.eq("state",BaseState.normal.getCode()));
        return baseService.findList(BusinessUsePhone.class,expressionList);
    }

    @Override
    public String findTestMsg() {
        String[] calls={"兄弟，"
                ,"朋友，",
                "帅哥，",
                "美女，",
                "哈，"};
        String[] msgs={"一切都好？",
                "近况如何？",
                "有什么新鲜事？",
                "你过得怎么样？>",
                "你好吗？",
                "好久不见？",
                "很高兴见到你？",
                "在忙什么？",
                "中午好!",
                "早上好!",
                "晚上好!",
                "吃饭了没？",
                "在哪里见？"};
        String[] icons={
                "<icon1>",
                "<icon1><icon2>",
                "<icon1><icon2><icon3>",
                "<icon1><icon2><icon3><icon4>",
                "<icon1><icon2><icon3><icon4><icon5>"};
        Random random =new Random();
        String call=calls[random.nextInt(calls.length)];
        String msg=msgs[random.nextInt(msgs.length)];
        String icon=icons[random.nextInt(icons.length)];
        return call+msg+icon;
    }
}
